<template>
  <div class="withdraw-container">
    <!-- 状态统计 -->
    <el-card class="status-card">
      <div class="status-list">
        <div
          class="status-item"
          :class="{ active: activeStatus === 'all' }"
          @click="handleStatusChange('all')"
        >
          <div class="label">全部</div>
          <div class="value">{{ statistics.total }}</div>
        </div>
        <div
          class="status-item"
          :class="{ active: activeStatus === 'pending' }"
          @click="handleStatusChange('pending')"
        >
          <div class="label">申请中</div>
          <div class="value">{{ statistics.pending }}</div>
        </div>
        <div
          class="status-item"
          :class="{ active: activeStatus === 'success' }"
          @click="handleStatusChange('success')"
        >
          <div class="label">已成功</div>
          <div class="value">{{ statistics.success }}</div>
        </div>
        <div
          class="status-item"
          :class="{ active: activeStatus === 'rejected' }"
          @click="handleStatusChange('rejected')"
        >
          <div class="label">已拒绝</div>
          <div class="value">{{ statistics.rejected }}</div>
        </div>
      </div>
    </el-card>

    <!-- 操作按钮 -->
    <div class="operation-bar">
      <el-button type="primary" @click="handleWithdraw">立即提现</el-button>
      <el-button type="primary" @click="handleAddAccount">添加提现账户</el-button>
    </div>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="序号" width="80" />
        <el-table-column prop="amount" label="金额" width="120" />
        <el-table-column prop="fee" label="手续费" width="120" />
        <el-table-column prop="tax" label="税费" width="120" />
        <el-table-column prop="type" label="业务类型" width="120" />
        <el-table-column prop="account" label="提现账户" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="status" label="提现状态" width="120">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'

// 状态统计数据
const statistics = ref({
  total: 60,
  pending: 10,
  success: 5,
  rejected: 5
})

// 当前选中的状态
const activeStatus = ref('all')

// 表格数据
const tableData = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取表格数据
const getTableData = async () => {
  loading.value = true
  try {
    // TODO: 调用API获取数据
    const mockData = Array.from({ length: 10 }).map((_, index) => ({
      id: index + 1,
      amount: 10.0,
      fee: 0.0,
      tax: 10.0,
      type: '提现',
      account: '622848208884898123',
      createTime: '2022-04-10 12:00:03',
      status: ['已成功', '已拒绝', '已拒绝'][Math.floor(Math.random() * 3)]
    }))
    tableData.value = mockData
    total.value = 100
  } catch (error) {
    console.error('获取数据失败:', error)
  } finally {
    loading.value = false
  }
}

// 页面加载时获取数据
onMounted(() => {
  getTableData()
})

// 处理状态切换
const handleStatusChange = (status: string) => {
  activeStatus.value = status
  getTableData()
}

// 获取状态标签类型
const getStatusType = (status: string) => {
  const typeMap: Record<string, string> = {
    已成功: 'success',
    已拒绝: 'danger',
    申请中: 'warning'
  }
  return typeMap[status] || 'info'
}

// 处理分页变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableData()
}

// 处理提现操作
const handleWithdraw = () => {
  // TODO: 实现提现逻辑
}

// 处理添加账户
const handleAddAccount = () => {
  // TODO: 实现添加账户逻辑
}

// 处理查看详情
const handleView = (_row: any) => {
  // TODO: 实现查看详情逻辑
}
</script>

<style lang="scss" scoped>
.withdraw-container {
  padding: 20px;

  .status-card {
    margin-bottom: 20px;

    .status-list {
      display: flex;
      gap: 20px;

      .status-item {
        flex: 1;
        padding: 15px;
        text-align: center;
        cursor: pointer;
        border-radius: 4px;
        transition: all 0.3s;

        &:hover {
          background-color: #f5f7fa;
        }

        &.active {
          background-color: #ecf5ff;
          color: #409eff;

          .value {
            color: #409eff;
          }
        }

        .label {
          font-size: 14px;
          color: #606266;
          margin-bottom: 8px;
        }

        .value {
          font-size: 24px;
          font-weight: bold;
          color: #303133;
        }
      }
    }
  }

  .operation-bar {
    margin-bottom: 20px;
    display: flex;
    gap: 10px;
  }

  .table-card {
    .pagination-container {
      margin-top: 20px;
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
